package br.ufrj.ppgi.jemf_advanced_example.victim;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Victim;
import br.ufrj.ppgi.jemf.mobile.view.VictimView;
import br.ufrj.ppgi.jemf.utils.CustomConversions;
import br.ufrj.ppgi.jemf.utils.CustomFormat;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Victim detail screen.
 * This fragment is either contained in a {@link VictimListActivity} in two-pane mode (on tablets) or a {@link VictimDetailActivity} on handsets.
 */
public class VictimDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_VICTIM = "activated_victim";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "VICTIM";
    /**
     * The Victim content this fragment is showing.
     */
    private static Victim victim;
    /**
     * The Victim view holder.
     */
    private static VictimView victimView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public VictimDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Victim content specified by the fragment arguments.
        if (getArguments().containsKey(VictimDetailFragment.ARG_ID)) {
        	victim = getArguments().getParcelable(VictimDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Victim content
	    if (victim != null) {
	    	victimView = new VictimView(getActivity(), false);
	    	// Name
	    	victimView.setVictimNameText(victim.getName());
	    	// Gender
	    	victimView.setVictimGenderBool(CustomFormat.IntToBool(victim.getGender()));
	    	// Birth Date
	    	victimView.setVictimBirthDateText(victim.getBirthDateString());
	    	// Age
	    	victimView.setVictimAgeText(String.valueOf(victim.getAge()));
	    	// Status
	    	victimView.setVictimStatusText(victim.getStatusString());

	    	// Its necessary to add the VictimView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Victim View as a child of the ScrollView.
	    	scrollView.addView(victimView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Victim
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_VICTIM)) {
        	victim = savedInstanceState.getParcelable(STATE_VICTIM);
        }
	}





	/**
     * Get updated Victim
     * @return Victim
     */
	public static Victim getVictim() {
		if (BuildConfig.DEBUG) Log.i(VictimListActivity.TAG, "VictimDetailFragment ::: getVictim");
    	return victim;
    }

	/**
	 * Update Victim data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((victim != null) && (victimView != null)) {
    		if (BuildConfig.DEBUG) Log.i(VictimListActivity.TAG, "VictimDetailFragment ::: updateDataFromUI");
    		// Update Victim object with new data included by user through components
    		victim.setName(victimView.getVictimNameText());
    		victim.setGender(CustomFormat.BoolToInt(victimView.getVictimGenderBool()));
    		victim.setBirthDateString(victimView.getVictimBirthDateText());
    		victim.setAge(Integer.valueOf(victimView.getVictimAgeText()));
    		if (victimView.getVictimAgeText().isEmpty()) {
    			victim.setAge(0);
    		} else {
    			victim.setAge(CustomConversions.birthDateToAge(victim.getBirthDate()));
    	    	// Updating Age TO UI.
        		victimView.setVictimAgeText(String.valueOf(victim.getAge()));
    		}
    		victim.setStatusString(victimView.getVictimStatusText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Victim.
        outState.putParcelable(STATE_VICTIM, victim);
    }
}
